﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using nwrole.Module.lib;
using nwrole.toolbox.sync.ServiceHost;

namespace nwrole.toolbox.sync.DestinationModule
{
    public class SyncDestinationModule : IModule
    {
        private AutoResetEvent _waitEvent = new AutoResetEvent(false);

        [ModuleConfig(60)]
        public int SyncIntervalSeconds { get; set; }

        [ModuleConfig(600)]
        public int CommandTimeout { get; set; }

        [ModuleConfig]
        public string DestDbConnectionString { get; set; }

        // Destination Service Connection Info

        [ModuleConfig(Name="SyncDestinationService/ConnType")]
        public string SyncDestServiceConnType { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/ServiceNamespace")]
        public string SyncDestServiceSBServiceNamespace { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/ServicePath")]
        public string SyncDestServiceSBServicePath { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/IssuerName")]
        public string SyncDestServiceSBIssuerName { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/ServiceBus/IssuerSecret")]
        public string SyncDestServiceSBIssuerSecret { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/WebService/BaseAddress")]
        public string SyncDestServiceWSBaseAddress { get; set; }

        [ModuleConfig(Name = "SyncDestinationService/WebService/Address")]
        public string SyncDestServiceWSAddress { get; set; }

        public void Start(IModuleDebug debug)
        {
            debug.RegisterLogger(new ModuleFileLogger(LogLevel.Critical));
            debug.RegisterLogger(new ModuleConsoleLogger(LogLevel.Info));
            debug.RegisterLogger(new ModuleEventLogger(LogLevel.Info));

            Trace.TraceInformation("Starting SyncDestinationModule");

            debug.Pulse("Starting");

            SyncDestinationServiceHost destHost = new SyncDestinationServiceHost();
            destHost.Start(SyncDestinationConnInfo, DestDbConnectionString);

            Trace.TraceInformation("SyncDestinationModule running");

            _waitEvent.WaitOne();
        }

        public void Stop()
        {
            Trace.TraceInformation("Stopping SyncDestinationModule");

            _waitEvent.Set();
        }

        private ServiceConnectionInfo SyncDestinationConnInfo
        {
            get
            {
                ServiceConnectionInfo connInfo = new ServiceConnectionInfo();

                ServiceConnectionInfo.ServiceConnectionType type;
                ServiceConnectionInfo.ServiceConnectionType.TryParse(SyncDestServiceConnType, true, out type);
                connInfo.Type = type;

                connInfo.ServiceBusServiceNamespace = SyncDestServiceSBServiceNamespace;
                connInfo.ServiceBusServicePath = SyncDestServiceSBServicePath;
                connInfo.ServiceBusIssuerName = SyncDestServiceSBIssuerName;
                connInfo.ServiceBusIssuerSecret = SyncDestServiceSBIssuerSecret;

                connInfo.WebServiceBaseAddress = SyncDestServiceWSBaseAddress;
                connInfo.WebServiceAddress = SyncDestServiceWSAddress;

                return connInfo;
            }
        }
    }
}
